package com.cykj.v_m_sprintboot.controller;

import com.cykj.v_m_sprintboot.bean.TbInput;
import com.cykj.v_m_sprintboot.bean.TbOut;
import com.cykj.v_m_sprintboot.service.TbInputService;
import com.cykj.v_m_sprintboot.service.TbOutService;
import com.cykj.v_m_sprintboot.tool.GetTime;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class OutController {
    @Autowired
    private TbOutService tbOutService;
    @Autowired
    private TbInputService tbInputService;
    //通过入库单获取仓库的名称
    @RequestMapping("OutGetWarehouseName")
    @ResponseBody
    public Map<String,Object>getInputWarehouseName(){
        Map<String,Object> map=new HashMap<>();
        List<TbOut> warehouseNameList= tbOutService.getInputWarehouseName();
        map.put("warehouseNameList",warehouseNameList);
        return map;
    }
    //获取通过入库单中仓库id获取的供应商名称
    @RequestMapping("outGetSupplier")
    @ResponseBody
    public Map<String,Object> outGetSupplier(@RequestBody TbOut tbOut){
        System.out.println("仓库的id："+tbOut.getWarehouseId());
        Map<String,Object> map=new HashMap<>();
        List<TbOut> supplierList=tbOutService.outGetSupplier((int)tbOut.getWarehouseId());
        map.put("supplierList",supplierList);
        return map;
    }
    //通过入库单的供应商Id获取商品的分类
    @RequestMapping("outGtGoodsType")
    @ResponseBody
    public Map<String,Object>outGtGoodsType(@RequestBody TbOut tbOut){
        Map<String,Object> map=new HashMap<>();
        List<TbOut> goodsTypeList=tbOutService.outGtGoodsType(tbOut);
        map.put("goodsTypeList",goodsTypeList);
        return map;
    }
    //通过入库单的商品分类获取和供应商的id获取商品名称
    @RequestMapping("OutGetGoodsName")
    @ResponseBody
    public Map<String,Object>OutGetGoodsName(@RequestBody TbOut tbOut){
        System.out.println(tbOut.getGoodsType()+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
         String[] strings=new String[2];
    if (tbOut.getGoodsType().equals("饮料")||tbOut.getGoodsType().equals("酒类")){
        strings[0]="件";
        strings[1]="瓶";
    }else {
        strings[0]="件";
        strings[1]="包";
    }

        Map<String,Object> map=new HashMap<>();
        List<TbOut> goodsList=tbOutService.OutGetGoodsName(tbOut);
        map.put("goodsList",goodsList);
        map.put("goodsUnit",strings);
        return map;
    }
    //通过入库单的商品名称获取和供应商的id获取商品的数量
    @RequestMapping("outGetGoodsNum")
    @ResponseBody
    public Map<String,Object>outGetGoodsNum(@RequestBody TbOut tbOut){
        System.out.println(tbOut.getGoodsName()+","+tbOut.getGoodsType()+","+tbOut.getSupplierId());
        Map<String,Object> map=new HashMap<>();
        double dataNum=tbOutService.countTbWarehouseListInf(tbOut);
        map.put("dataNum",dataNum);
        return map;
    }
    //判断商品的数量是否充实
    @RequestMapping("checkGoodsNum")
    @ResponseBody
    public Map<String,Object> checkGoodsNum(@RequestBody TbOut tbOut){
        Map<String,Object> map=new HashMap<>();
        System.out.println(tbOut.getGoodsName()+","+tbOut.getWarehouseId()+","+tbOut.getGoodsType()+","+tbOut.getSupplierId()+","+tbOut.getOutNum()+","+tbOut.getGoodsUnit());

        //查找运行商的名称
        List<TbOut> operatorList =tbOutService.selectOperator(1);
        map.put("operatorList",operatorList);
        return map;
    }
    //出库数据提交
    @RequestMapping("addOutData")
    @ResponseBody
    public String addOutData(@RequestBody TbOut tbOut){
        tbOut.setModifyTime(GetTime.getTime());
        //单位的换算
        double num=0;
        if (tbOut.getGoodsUnit().equals("瓶") ||tbOut.getGoodsUnit().equals("包")){
            num=tbOut.getOutNum()/10;
        }else{
            num=tbOut.getOutNum();
        }
        //计算库存是否充足
        double totalNum=tbOutService.countTbWarehouseListInf(tbOut);
        tbOut.setGoodsNum(totalNum-num);
        if (tbOut.getGoodsNum()>=0){
            //总库存数量减少
            tbOutService.updateTbWarehouseListInfNum(tbOut);
            //出库单生成
            tbOutService.addOutData(tbOut);
            return "出库成功";
        }
        return "出库数量大于实际数量，请重新输入";


    }
    //获取出库数据展示
    @RequestMapping("getOutInf")
    @ResponseBody
    public Map<String,Object>getOutInf(@RequestBody TbOut pageInf){
        pageInf.setStateListId(1);
        pageInf.setGoodsUnit("件");
        RowBounds row =new RowBounds((pageInf.getPage()-1)*pageInf.getSize(),pageInf.getSize());
        Map<String,Object> map=new HashMap<>();
        List<TbOut> outInf=tbOutService.outInf(pageInf,row);
        map.put("count",tbOutService.count(pageInf));
        map.put("outInf",outInf);
        return map;
    }
    //删除出库单
    @RequestMapping("delOut")
    @ResponseBody
    public String delOut(@RequestBody TbOut tbOut){
        double num=0;
        if (tbOut.getGoodsUnit().equals("瓶") ||tbOut.getGoodsUnit().equals("包")){
            num=tbOut.getGoodsNum()/10;
        }else{
            num=tbOut.getGoodsNum();
        }
        tbOutService.delOut(tbOut);
        //数据删除后出库的数据量要返回到入库的商品
        tbOut.setModifyTime(GetTime.getTime());
        double count=tbOutService.countTbWarehouseListInf(tbOut);
        System.out.println("仓库为："+count+"出库数量："+num+"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        tbOut.setGoodsNum(count+num);
        tbOutService.updateTbWarehouseListInfNum(tbOut);
        return "移除成功";
    }



}
